package genericity

import (
	"newRetailByGolang/utils"
	"newRetailByGolang/variable"
	"reflect"
	"time"
)

type GenericUnified[T any] struct {
	Pojo T
}

func (gu *GenericUnified[any]) Created() {
	//variable.Db.Create(&(gu.Pojo))

	value := reflect.ValueOf(&(gu.Pojo))
	var vParams []reflect.Value
	vParams = append(vParams, value)
	value.MethodByName("InsertOrder").Call(vParams)

	uw := new(utils.Worker)

	npValue := reflect.ValueOf(&(gu.Pojo)).Elem()
	npValue.FieldByName("Id").SetInt(uw.GetId() + 666)
	npValue.FieldByName("UserId").SetInt(uw.GetId())
	npValue.FieldByName("UserName").SetString("genericity Value值 新增 UserName")
	npValue.FieldByName("GoodId").SetInt(uw.GetId())
	npValue.FieldByName("GoodName").SetString("genericity Value值 新增 GoodName")
	npValue.FieldByName("CreatedAt").Set(reflect.ValueOf(time.Now()))
	npValue.FieldByName("UpdatedAt").Set(reflect.ValueOf(time.Now()))
	var npvParams []reflect.Value
	npvParams = append(npvParams, npValue)
	npValue.MethodByName("InsertOrderByNoPointer").Call(npvParams)

	to := reflect.New(reflect.TypeOf(gu.Pojo))
	tValue := to.Elem()
	tValue.FieldByName("Id").SetInt(uw.GetId() + 111)
	tValue.FieldByName("UserId").SetInt(uw.GetId())
	tValue.FieldByName("UserName").SetString("genericity 属性 新增 UserName")
	tValue.FieldByName("GoodId").SetInt(uw.GetId())
	tValue.FieldByName("GoodName").SetString("genericity 属性 新增 GoodName")
	tValue.FieldByName("CreatedAt").Set(reflect.ValueOf(time.Now()))
	tValue.FieldByName("UpdatedAt").Set(reflect.ValueOf(time.Now()))
	var params []reflect.Value
	params = append(params, tValue)
	to.MethodByName("InsertOrderByInterface").Call(params)

	pto := reflect.New(reflect.TypeOf(&(gu.Pojo)).Elem())
	ptValue := pto.Elem()
	ptValue.FieldByName("Id").SetInt(uw.GetId() + 555)
	ptValue.FieldByName("UserId").SetInt(uw.GetId())
	ptValue.FieldByName("UserName").SetString("genericity 属性 指针方法 新增 UserName")
	ptValue.FieldByName("GoodId").SetInt(uw.GetId())
	ptValue.FieldByName("GoodName").SetString("genericity 属性 新增 指针方法 GoodName")
	ptValue.FieldByName("CreatedAt").Set(reflect.ValueOf(time.Now()))
	ptValue.FieldByName("UpdatedAt").Set(reflect.ValueOf(time.Now()))
	var pParams []reflect.Value
	pParams = append(pParams, pto)
	ptValue.MethodByName("InsertOrderByStruct").Call(pParams)
}

func (gu *GenericUnified[any]) Delete(id int64) {
	variable.Db.Delete(&(gu.Pojo), id)
}

func (gu *GenericUnified[any]) Update() {
	variable.Db.Save(&(gu.Pojo))
}

type Muster[T any] []*T

//func (gu *GenericUnified[any]) Query(id int64) Muster[any] {
//	var entity Muster[any]
//	variable.Db.Where(id).First(&entity)
//	return entity
//}

func (gu *GenericUnified[any]) QueryAll(condition map[string]interface{}) Muster[any] {
	var entities Muster[any]
	variable.Db.Where(condition).Find(&entities)

	return entities
}

// Created Delete Update Query QueryAll 泛型函数版本
func Created[T any](t T) {
	variable.Db.Create(&t)

	uw := new(utils.Worker)
	to := reflect.New(reflect.TypeOf(t))
	tValue := to.Elem()
	tValue.FieldByName("Id").SetInt(uw.GetId() + 444)
	tValue.FieldByName("UserId").SetInt(uw.GetId())
	tValue.FieldByName("UserName").SetString("genericity function 属性 新增 UserName")
	tValue.FieldByName("GoodId").SetInt(uw.GetId())
	tValue.FieldByName("GoodName").SetString("genericity function 属性 新增 GoodName")
	tValue.FieldByName("CreatedAt").Set(reflect.ValueOf(time.Now()))
	tValue.FieldByName("UpdatedAt").Set(reflect.ValueOf(time.Now()))
	var params []reflect.Value
	//nt := tValue.Interface().(*T)
	nt := tValue.Interface().(T)
	params = append(params, reflect.ValueOf(nt))
	to.MethodByName("InsertOrderByInterface").Call(params)
}

func Delete[T any](t T, id int64) {
	variable.Db.Delete(&t, id)
}

func Update[T any](t T) {
	variable.Db.Save(&t)
}

func Query[T any](entity T, id int64) T {
	variable.Db.Where(id).First(&entity)
	return entity
}

func QueryAll[T any](entities []T, condition map[string]interface{}) []T {
	variable.Db.Where(condition).Find(&entities)
	return entities
}







